package me.shurufa.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import de.greenrobot.event.EventBus;
import me.shurufa.R;
import me.shurufa.holder.BaseViewHolder;
import me.shurufa.utils.Constants;
import me.shurufa.utils.Utils;

/**
 * 作者 : andy
 * 日期 : 16/3/8 14:50
 * 邮箱 : andyxialm@gmail.com
 * 描述 : 左侧菜单列表适配器
 */
public class MenuListAdapter extends RecyclerView.Adapter<MenuListAdapter.MenuViewHolder> {

    private int mSelectedIndex;
    private List<String> mDatas;
    private Activity mActivity;
    public MenuListAdapter(Activity activity, List<String> datas, int selectedIndex) {
        mActivity = activity;
        this.mDatas = datas;
        this.mSelectedIndex = selectedIndex;
    }

    @Override
    public MenuViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.list_item_menu, parent, false);
        return new MenuViewHolder(v);
    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    @Override
    public void onBindViewHolder(MenuViewHolder holder, final int position) {
        holder.menuTv.setText(mDatas.get(position));
        holder.indicator.setVisibility(position == mSelectedIndex ? View.VISIBLE : View.GONE);

        holder.item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (position == 3 || position == 4 || position == 5) {
                    if (!Utils.checkLogin(mActivity)) {
                        Utils.showToast(R.string.login_id);
                        return;
                    }
                }

                Intent in = new Intent(Constants.ACTION_CHANGE_MENU);
                mSelectedIndex = position;
                in.putExtra(Constants.EXTRA_SWITCH_INDEX, mSelectedIndex);
                EventBus.getDefault().post(in);
            }
        });
    }

    public void notifySelectedIndex(int index) {
        mSelectedIndex = index;
        notifyDataSetChanged();
    }

    public class MenuViewHolder extends BaseViewHolder {
        @Bind(R.id.item)
        RelativeLayout item;
        @Bind(R.id.tv_menu)
        TextView menuTv;
        @Bind(R.id.indicator)
        View indicator;
        public MenuViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

    }

}
